package com.ly.hotel.timetask;

import android.text.TextUtils;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.Weather;
import com.ly.hotel.model.WeatherMsg;
import com.ly.hotel.msgevent.ActionTimeTickEvent;
import com.ly.hotel.service.SPABackService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.TimerTask;

import de.greenrobot.event.EventBus;


public class WeatherTimerTask extends TimerTask{
	private static MyApplication app = (MyApplication) MyApplication.getContext();
	@Override
	public void run() {
		try {
			/*if (!TextUtils.isEmpty(app.areaId)) {
				*//*String url = "http://tj.nineton.cn/Heart/index/all?city=" + app.areaId;*//*
				StringBuilder sb = new StringBuilder(app.areaId);
				String newId = sb.replace(app.areaId.length() - 3,app.areaId.length() + 1,"000").toString();
				String url = "https://api.seniverse.com/v3/weather/daily.json?key=5ovomx2kui2x6ayt&location=" +
						newId + "&language=zh-Hans&unit=c&start=0&days=3";
				StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
						new Response.Listener<String>() {
							@Override
							public void onResponse(String response) {
								Log.i("weather","weather=" +response);
								try {
									JSONObject jsonObject = new JSONObject(response);
									JSONArray jsonArray = jsonObject.getJSONArray("results");
									JSONObject jsonObject2 = jsonArray.getJSONObject(0);

									JSONArray dailyArray = jsonObject2.getJSONArray("daily");
									app.weathers.clear();

									for(int i = 0;i < dailyArray.length();i ++){
										if(!TextUtils.isEmpty(app.areaName)){
											JSONObject object = dailyArray.getJSONObject(i);
											Weather weather = new Weather();
											weather.setArea(app.areaName.split(";")[0]);
											weather.setAreaEn(app.areaName.split(";")[1]);
											weather.setDate(object.getString("date"));
											weather.setwCode(object.getString("code_day"));
											weather.setwCode2(object.getString("code_night"));
											weather.setLow(object.getString("low"));
											weather.setHeight(object.getString("high"));
											app.weathers.add(weather);
										}
									}
									if(app.weathers.size() > 0){
										WeatherMsg wm = app.getWeatherName(app.weathers.get(0).getwCode());
										app.nowWeather = new WeatherMsg(wm.getCode(),wm.getName(),wm.getNameEn(),wm.getImg());
										app.nowWeather.setTemperature(app.weathers.get(0).getLow() + "-" +
												app.weathers.get(0).getHeight());
									}
									EventBus.getDefault().post(new ActionTimeTickEvent());
								} catch (JSONException e) {
									e.printStackTrace();
								}
								*//*try {
									JSONObject jsObj = new JSONObject(response);
									JSONArray jArr = jsObj.getJSONArray("weather");
									JSONObject jsObj2 = jArr.getJSONObject(0);
									JSONObject localObj = jsObj2.getJSONObject("now");

									WeatherMsg wm = app.getWeatherName(localObj.getString("code"));
									app.nowWeather = new WeatherMsg(wm.getCode(),wm.getName(),wm.getNameEn(),wm.getImg());
									app.nowWeather.setTemperature(localObj.getString("temperature"));

									JSONArray dailyJArr = jsObj2.getJSONArray("future");
									app.weathers.clear();

									for (int i =0; i <dailyJArr.length(); i++){
										JSONObject obj = dailyJArr.getJSONObject(i);
										Weather weather = new Weather();
										weather.setArea(app.areaName.split(";")[0]);
										weather.setAreaEn(app.areaName.split(";")[1]);
										weather.setDate(obj.getString("date"));
										weather.setwCode(obj.getString("code1"));
										weather.setwCode2(obj.getString("code2"));
										weather.setLow(obj.getString("low"));
										weather.setHeight(obj.getString("high"));
										app.weathers.add(weather);
									}
									EventBus.getDefault().post(new ActionTimeTickEvent());
								} catch (JSONException e) {
									e.printStackTrace();
								}*//*
							}
						}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e("weather", "获取天气失败." + error.getMessage());
					}
				});
				stringRequest.setTag("weather");
				stringRequest.setShouldCache(false);
				SPABackService.mQueue.add(stringRequest);
			}*/
		} catch (Exception e) {
			Log.e("weather", "weather:" + e.getMessage());
		}
	}

}
